/*
 * InterlockedCompareExchange.h
 *
 *  Created on: 2011/11/29
 *      Author: naoki
 */

#ifndef INCLUDED_NTENGINE_INTERLOCKEDCOMPAREEXCHANGE_H
#define INCLUDED_NTENGINE_INTERLOCKEDCOMPAREEXCHANGE_H

#include <sys/cdefs.h>
#include <sys/time.h>

namespace NTEngine {

/**
 * アトミックなCAS
 *
 * @param dest 対象のアドレス
 * @param exch 入れ替える値
 * @param comp 比較する値
 * @return 変化前の値
 */
int InterlockedCompareExchange( volatile int* dest, int exch, int comp );

#define __ATOMIC_INLINE__ static __inline__ __attribute__((always_inline))

__ATOMIC_INLINE__ int
__atomic_cmpxchg( int old, int _new, volatile int* ptr ) {
	// We must return 0 on success
    return ( InterlockedCompareExchange( ptr, _new, old ) != old );
}

} // namespace NTEngine

#endif // INCLUDED_NTENGINE_INTERLOCKEDCOMPAREEXCHANGE_H
